-module(test).
-export([demo1/0, demo2/0]).

-define(LOG_PATH, "test.log").

%% <1> ejabberd的logger模块依赖于dynamic_compile模块
%% <2> 在使用ejabberd_logger模块前必须调用, ejabber_loglevel:set/1函数.
%%     因为ejabber_logger模块是动态生成的, 当我们调用ejabberd_loglevel:set/1
%%     设置level的时候, 会动态生成一个ejabberd_logger模块. 当再次调用set/1来修改
%%     level的时候, 会生成一个新的ejabberd_logger模块.
%% 


%% 使用ejabberd_logger
demo1() ->
    %% scenario - 1
    ejabberd_loglevel:set(3), %% warning, debug和info将不会被输出
    ejabberd_logger:debug_msg(?MODULE, ?LINE, "this is ~p msg~n", ["debug"]),
    ejabberd_logger:info_msg(?MODULE, ?LINE, "this is ~p msg~n", ["info"]),
    ejabberd_logger:warning_msg(?MODULE, ?LINE, "this is ~p msg~n", ["warning"]),
    ejabberd_logger:error_msg(?MODULE, ?LINE, "this is ~p msg~n", ["error"]),
    ejabberd_logger:critical_msg(?MODULE, ?LINE, "this is ~p msg~n", ["critical"]),

    %% scenario - 2
    ejabberd_loglevel:set(5), %% debug, 所有的信息都会输出
    ejabberd_logger:debug_msg(?MODULE, ?LINE, "this is ~p msg~n", ["debug"]),
    ejabberd_logger:info_msg(?MODULE, ?LINE, "this is ~p msg~n", ["info"]),
    ejabberd_logger:warning_msg(?MODULE, ?LINE, "this is ~p msg~n", ["warning"]),
    ejabberd_logger:error_msg(?MODULE, ?LINE, "this is ~p msg~n", ["error"]),
    ejabberd_logger:critical_msg(?MODULE, ?LINE, "this is ~p msg~n", ["critical"]).

demo2() ->
    error_logger:add_report_handler(ejabberd_logger_h, ?LOG_PATH),
    error_logger:info_msg("this is a test msg").



